<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maxSumTwoNoOverlap = function (A, L, M) {
      const len = A.length
      const prefixSum = Array(len + 1).fill(0)
      for (let i = 0; i < len; i++) {
        prefixSum[i + 1] = prefixSum[i] + A[i]
      }

      let ans = 0
      for (let i = 0; i <= len - L; i++) {
        const l = prefixSum[i + L] - prefixSum[i]
        if (i - M >= 0) {
          for (let j = 0; j <= i - M; j++) {
            const r = prefixSum[j + M] - prefixSum[j]
            ans = Math.max(ans, l + r)
          }
        }
        for (let j = i + L; j <= len - M; j++) {
          const r = prefixSum[j + M] - prefixSum[j]
          ans = Math.max(ans, l + r)
        }
      }
      return ans
    }
    console.log(maxSumTwoNoOverlap([0, 6, 5, 2, 2, 5, 1, 9, 4], 1, 2))
  </script>
</body>

</html>